package com.panku.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.panku.MyRequestMethod;
import com.panku.annocation.myController;
import com.panku.annocation.myMethod;
import com.panku.core.entity.ClassBean;

/**
 * 进行数据转储
 * @author jack
 *
 */
public class DataUtil {

	
	
	/**
	 * 从注解中获取类的映射关系
	 * @param packPath 包的字符串数组
	 */
	public static Map<String, ClassBean> readDataFromZJ(String[] packPath){
		Map<String, ClassBean> pathList=new HashMap<>();
		//扫描给定包下的包含注解的类和类中包含注解的方法
		ClassScanner cs=new ClassScanner();
		for (int i = 0; i < packPath.length; i++) {
			cs.scanning(packPath[i], true);
		}
		Map<String, Class<?>> classes = cs.getClasses();
		Iterator<Entry<String, Class<?>>> it=classes.entrySet().iterator();
		while (it.hasNext()) {
			Class<?> clazz = it.next().getValue();
			//如果类中包含控制器注解则迭代类中的方法放入集合中
			if (clazz.isAnnotationPresent(myController.class)) {
				String ppath=clazz.getAnnotation(myController.class).value();
				Method[] methods=clazz.getMethods();
				for (Method method : methods) {
					if (method.isAnnotationPresent(myMethod.class)) {
						String mpath=method.getAnnotation(myMethod.class).value();
						String totalPath=ppath+mpath;
						System.out.println("============================请求路径为:"+ppath+mpath);
						System.out.println("++++++++++++++++++++++++"+clazz.getName());
						MyRequestMethod resMethod=method.getAnnotation(myMethod.class).method();
						//将数据封装到实体bean中
						ClassBean cb=new ClassBean(clazz,method,resMethod);
						pathList.put(totalPath, cb);
					}
				}
			}
		}
		return pathList;
	}
	
	public static void main(String[] args) {
		readDataFromZJ(new String[]{"com.panku.controller"});
	}
	
}
